<template>
  <div class="navigation">
    <el-container>
      <el-header class="nav-header">
        <div class="nav-brand">
          <span class="brand-name">房地产平台</span>
        </div>
        
        <div class="nav-menu">
          <el-menu
            :default-active="activeIndex"
            mode="horizontal"
            @select="handleSelect"
            class="nav-menu-list"
          >
            <el-menu-item index="/">
              <el-icon><House /></el-icon>
              <span>首页</span>
            </el-menu-item>
            
            <el-menu-item index="/properties">
              <el-icon><Search /></el-icon>
              <span>房源浏览</span>
            </el-menu-item>
            
            <!-- 房源管理 -->
            <el-menu-item index="/publish-property" v-if="authStore.isLoggedIn">
              <el-icon><Plus /></el-icon>
              <span>发布房源</span>
            </el-menu-item>
            
            <el-menu-item index="/my-properties" v-if="authStore.isLoggedIn">
              <el-icon><Grid /></el-icon>
              <span>我的房源</span>
            </el-menu-item>
            
            <!-- 个人中心 -->
            <el-menu-item index="/dashboard" v-if="authStore.isLoggedIn">
              <el-icon><UserFilled /></el-icon>
              <span>个人信息</span>
            </el-menu-item>
            
            <el-menu-item index="/appointments" v-if="authStore.isLoggedIn">
              <el-icon><Calendar /></el-icon>
              <span>我的预约</span>
            </el-menu-item>
            
            <el-menu-item index="/contracts" v-if="authStore.isLoggedIn">
              <el-icon><Document /></el-icon>
              <span>我的合同</span>
            </el-menu-item>
            
            <el-menu-item index="/requirements" v-if="authStore.isLoggedIn">
              <el-icon><Edit /></el-icon>
              <span>我的需求</span>
            </el-menu-item>
            
            <!-- 帮助功能 -->
            <el-menu-item index="/feedback" v-if="authStore.isLoggedIn">
              <el-icon><ChatDotRound /></el-icon>
              <span>意见反馈</span>
            </el-menu-item>
            
            <el-menu-item index="/messages" v-if="authStore.isLoggedIn">
              <el-icon><Bell /></el-icon>
              <span>系统消息</span>
            </el-menu-item>
            
            <el-menu-item index="/contact" v-if="authStore.isLoggedIn">
              <el-icon><Phone /></el-icon>
              <span>联系客服</span>
            </el-menu-item>
          </el-menu>
        </div>
        
        <div class="nav-actions">
          <div v-if="!authStore.isLoggedIn" class="auth-buttons">
            <el-button @click="$router.push('/login')">登录</el-button>
            <el-button type="primary" @click="$router.push('/register')">注册</el-button>
          </div>
          
          <div v-else class="user-info">
            <div class="user-avatar">
              <el-avatar :size="32" :src="authStore.user?.avatar">
                {{ authStore.user?.username?.charAt(0) }}
              </el-avatar>
              <span class="username">{{ authStore.user?.username }}</span>
            </div>
            <el-button @click="handleLogout" type="danger" size="small">
              <el-icon><SwitchButton /></el-icon>
              退出
            </el-button>
          </div>
        </div>
      </el-header>
    </el-container>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { useAuthStore } from '@/store/auth'
import { ElMessage } from 'element-plus'

const router = useRouter()
const route = useRoute()
const authStore = useAuthStore()

const activeIndex = computed(() => {
  const path = route.path
  if (path === '/') return '/'
  if (path.startsWith('/properties')) return '/properties'
  if (path.startsWith('/publish-property')) return '/publish-property'
  if (path.startsWith('/my-properties')) return '/my-properties'
  if (path.startsWith('/dashboard')) return '/dashboard'
  if (path.startsWith('/appointments')) return '/appointments'
  if (path.startsWith('/contracts')) return '/contracts'
  if (path.startsWith('/requirements')) return '/requirements'
  if (path.startsWith('/feedback')) return '/feedback'
  if (path.startsWith('/messages')) return '/messages'
  if (path.startsWith('/contact')) return '/contact'
  return path
})

const handleSelect = (index) => {
  if (index.startsWith('/')) {
    router.push(index)
  }
}

const handleLogout = async () => {
  await authStore.logout()
  ElMessage.success('已退出登录')
  router.push('/')
}
</script>

<style scoped>
.navigation {
  background: #fff;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
  position: sticky;
  top: 0;
  z-index: 1000;
}

.nav-header {
  display: flex;
  align-items: center;
  height: 64px;
  padding: 0 24px;
  max-width: 1200px;
  margin: 0 auto;
}

.nav-brand {
  flex-shrink: 0;
  margin-right: 40px;
}

.brand-name {
  font-size: 20px;
  font-weight: 600;
  color: #333;
}

.nav-menu {
  flex: 1;
  display: flex;
  justify-content: flex-start;
  overflow-x: auto;
  scrollbar-width: none;
  -ms-overflow-style: none;
}

.nav-menu::-webkit-scrollbar {
  display: none;
}

.nav-menu-list {
  border-bottom: none;
  white-space: nowrap;
  flex-shrink: 0;
}

.nav-menu-list .el-menu-item {
  height: 64px;
  line-height: 64px;
  padding: 0 12px;
  display: inline-flex;
  align-items: center;
  gap: 4px;
  white-space: nowrap;
  min-width: auto;
}

.nav-actions {
  display: flex;
  align-items: center;
  gap: 16px;
  flex-shrink: 0;
  margin-left: 20px;
}

.auth-buttons {
  display: flex;
  gap: 8px;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 12px;
}

.user-avatar {
  display: flex;
  align-items: center;
  gap: 8px;
}

.username {
  font-weight: 500;
  color: #333;
}

:deep(.el-menu--horizontal .el-menu-item) {
  border-bottom: 2px solid transparent;
}

:deep(.el-menu--horizontal .el-menu-item.is-active) {
  border-bottom-color: #409eff;
  background-color: transparent;
}

:deep(.el-menu--horizontal .el-sub-menu .el-sub-menu__title) {
  border-bottom: 2px solid transparent;
}

:deep(.el-menu--horizontal .el-sub-menu.is-active .el-sub-menu__title) {
  border-bottom-color: #409eff;
}

@media (max-width: 768px) {
  .nav-header {
    padding: 0 16px;
  }
  
  .nav-brand {
    margin-right: 16px;
  }
  
  .nav-menu {
    flex: 1;
  }
  
  .nav-menu-list .el-menu-item {
    padding: 0 8px;
    font-size: 14px;
  }
  
  .nav-menu-list .el-menu-item span {
    display: none;
  }
  
  .brand-name {
    font-size: 16px;
  }
  
  .user-info .username {
    display: none;
  }
  
  .user-info {
    gap: 8px;
  }
  
  .nav-actions {
    margin-left: 8px;
  }
}

@media (max-width: 480px) {
  .nav-menu-list .el-menu-item {
    padding: 0 6px;
  }
  
  .nav-actions {
    gap: 8px;
  }
}
</style>
